/*
 * Created by JFormDesigner on Thu Nov 07 13:18:11 CST 2024
 */

package com.hafu.view.kehu;

import java.awt.event.*;
import com.hafu.model.Khinfo;
import com.hafu.service.KhinfoService;
import com.hafu.util.StringUtil;

import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.border.*;
import javax.swing.table.*;

/**
 * @author 25922
 */
public class ManageKeHuFrm extends JFrame {
    private KhinfoService khinfoService = new KhinfoService();
    public ManageKeHuFrm() {
        initComponents();
        fillTable();
    }

    private void fillTable() {
        //
        DefaultTableModel dtm= (DefaultTableModel) table1.getModel();
        dtm.setRowCount(0);
        //获取数据
        ArrayList<Khinfo> khinfoList = khinfoService.getKhinfoByName(new Khinfo());
        for(Khinfo khinfo:khinfoList){
            Object[] row = {khinfo.getId(),khinfo.getName(),khinfo.getTel()};
            dtm.addRow(row);
        }
    }

    private void button1(ActionEvent e) {
        // TODO add your code here 查询事件
        DefaultTableModel dtm= (DefaultTableModel) table1.getModel();
        dtm.setRowCount(0);
        String name=textField1.getText();
        Khinfo khinfo = new Khinfo();
        khinfo.setName(name);
        ArrayList<Khinfo> khinfoByName = khinfoService.getKhinfoByName(khinfo);
        if (khinfoByName.size()>0){
            for(Khinfo khinfo1 : khinfoByName){
                Object[] rowDate = {khinfo1.getId(),khinfo1.getName(),khinfo1.getTel()};
                dtm.addRow(rowDate);
            }
        }else{
            JOptionPane.showMessageDialog(null,"没有找到该客户信息");
        }

    }

    private void table1MouseClicked(MouseEvent e) {
        // TODO add your code here 获取选中行
        int row = table1.getSelectedRow();  
        
        //得到行的数据
        DefaultTableModel dtm= (DefaultTableModel) table1.getModel();
        int choosedId = (int) dtm.getValueAt(row, 0);
        String name = (String)dtm.getValueAt(row, 1);
        String tel = (String)dtm.getValueAt(row, 2);
        
        textField2.setText(name);
        textField3.setText(tel);
        
    }
    private void button2(ActionEvent e) {
        // TODO add your code here 修改客户信息事件
        String name=textField2.getText();
        String tel=textField3.getText();
        DefaultTableModel dtm= (DefaultTableModel) table1.getModel();
        int row = table1.getSelectedRow();
        int id = (int) dtm.getValueAt(row, 0);
        //修改客户信息
        if(StringUtil.isEmpty(name)){
            JOptionPane.showMessageDialog(null,"客户名字不能为空");
        }
        if(StringUtil.isEmpty(tel)){
            JOptionPane.showMessageDialog(null,"客户电话不能为空");
        }
        Khinfo khinfo = new Khinfo(id,name,tel);
        Boolean updateKhinfo = khinfoService.updateKhinfo(khinfo);
        if(updateKhinfo){
            JOptionPane.showMessageDialog(null,"修改成功！");
            fillTable();
        }else{
            JOptionPane.showMessageDialog(null,"修改失败！");
        }


        
    }

    private void button3(ActionEvent e) {
        // TODO add your code here 删除事件
        int row = table1.getSelectedRow();
        DefaultTableModel dtm= (DefaultTableModel) table1.getModel();
        int id = (int) dtm.getValueAt(row, 0);
        Boolean deleteKhinfo = khinfoService.deleteKhinfo(id);
        if(deleteKhinfo){
            JOptionPane.showMessageDialog(null,"删除成功！");
            fillTable();
        }else{
            JOptionPane.showMessageDialog(null,"删除失败！");
        }
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        textField1 = new JTextField();
        button1 = new JButton();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        label2 = new JLabel();
        label3 = new JLabel();
        textField2 = new JTextField();
        textField3 = new JTextField();
        button2 = new JButton();
        button3 = new JButton();

        //======== this ========
        setTitle("\u7ba1\u7406\u5ba2\u6237\u8868\u5355");
        setBackground(new Color(0x999999));
        Container contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u5ba2\u6237\u540d\u5b57");

        //---- button1 ----
        button1.setText("\u67e5\u8be2");
        button1.addActionListener(e -> button1(e));

        //======== scrollPane1 ========
        {

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                    {null, "", ""},
                    {null, null, null},
                },
                new String[] {
                    "\u7f16\u53f7", "\u540d\u5b57", "\u7535\u8bdd"
                }
            ));
            table1.setBorder(null);
            table1.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    table1MouseClicked(e);
                    table1MouseClicked(e);
                }
            });
            scrollPane1.setViewportView(table1);
        }

        //---- label2 ----
        label2.setText("\u5ba2\u6237\u540d\u5b57");

        //---- label3 ----
        label3.setText("\u5ba2\u6237\u7535\u8bdd");

        //---- button2 ----
        button2.setText("\u4fee\u6539");
        button2.addActionListener(e -> button2(e));

        //---- button3 ----
        button3.setText("\u5220\u9664");
        button3.addActionListener(e -> button3(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(96, 96, 96)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addComponent(label1)
                            .addGap(40, 40, 40)
                            .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 203, GroupLayout.PREFERRED_SIZE)
                            .addGap(53, 53, 53)
                            .addComponent(button1, GroupLayout.PREFERRED_SIZE, 103, GroupLayout.PREFERRED_SIZE))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addComponent(label2)
                                .addComponent(label3))
                            .addGap(78, 78, 78)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                .addComponent(textField2, GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
                                .addComponent(textField3, GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE))
                            .addGap(94, 94, 94)
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addComponent(button2)
                                .addComponent(button3)))
                        .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 502, GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(135, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(60, 60, 60)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label1)
                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(button1))
                    .addGap(28, 28, 28)
                    .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 246, GroupLayout.PREFERRED_SIZE)
                    .addGap(37, 37, 37)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label2)
                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(button2))
                    .addGap(32, 32, 32)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label3)
                        .addComponent(textField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(button3))
                    .addContainerGap(31, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JTextField textField1;
    private JButton button1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JLabel label2;
    private JLabel label3;
    private JTextField textField2;
    private JTextField textField3;
    private JButton button2;
    private JButton button3;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
